<?PHP
include("../inc/config.php");
?>
<html>
<head>
	<title>Config crossfields</title>
	<style type="text/css">@import '../css/common.css';</style>
	<script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
	
	<div>
	
	<?PHP
	$_CONF->debugStatus = false;
	$crossNode = $_CONF->get('CONFIG>TABLES>TABLE.name='.$_SESSION['configuring_table'].'>FIELDS>FIELD.name='.$_GET['field'].'>CROSS');
	$crossTable = $_CONF->get('CROSS.table', $crossNode);
	if($_GET['crossTable']) $crossTable = $_GET['crossTable'];
	$crossRef = $_CONF->get('CROSS.ref', $crossNode);
	if($_GET['crossTableRef']) $crossRef = $_GET['crossTableRef'];
	$crossTitle = $_CONF->get('CROSS.title', $crossNode);
	if($_GET['crossTableLabel']) $crossTitle = $_GET['crossTableLabel'];
	
	?><table><tr valign="top"><td width="33%"><h2>Tabella a cui puntare</h2><select multiple="multiple" style="width:100%; max-height:270px; height:270px" onChange="window.location = '?field=<?=$_GET['field']?>&crossTable='+this.options[this.selectedIndex].value"><?
		$tables = $_DB->getTablesName();
		foreach($tables as $key => $table) {
			echo '<option value="'.$table.'" '.(($crossTable == $table)?"selected":"").'>'.$table.'</option>';
		}
	?></select></td><?
	
	if($crossTable !== false) {
		?><td width="33%"><h2>Campo bridge</h2><select multiple="multiple" style="width:100%" onChange="window.location = '?field=<?=$_GET['field']?>&crossTable=<?=$_GET['crossTable']?>&crossTableRef='+this.options[this.selectedIndex].value"><?
		foreach($_DB->getFields($crossTable) as $name => $type) {
			echo '<option value="'.$name.'" '.(($crossRef == $name)?"selected":"").'>'.$name.'</option>';		
		}
		?></select></td><?
	}
	
	if($_GET['crossTableRef'] || $crossRef !== false) {
		?><td width="33%"><h2>Campo da mostrare</h2><select multiple="multiple" style="width:100%" onChange="window.location = '?field=<?=$_GET['field']?>&crossTable=<?=$_GET['crossTable']?>&crossTableRef=<?=$_GET['crossTableRef']?>&crossTableLabel='+this.options[this.selectedIndex].value"><?
		foreach($_DB->getFields($crossTable) as $name => $type) {
			if($_GET['crossTableLabel'] == $name) $selected = "selected";
			else if($crossTitle == $name) $selected = "selected";
			else $selected = "";
			echo '<option value="'.$name.'" '.(($crossTitle == $name)?"selected":"").'>'.$name.'</option>';		
		}
		?></select></td><?
	}
	?>
	</tr></table>
	</div>
	
	<? 	if($_GET['crossTableLabel'] || $crossTitle !== false) { ?>
		<div class="popupButtonRow">
			<?
			if($_GET['action'] == 'SAVE') {
				
				$crossNode = $_CONF->get('CONFIG>TABLES>TABLE.name='.$_SESSION['configuring_table'].'>FIELDS>FIELD.name='.$_GET['field'].'.childNodes', false, array('createIfNull'));
				$crossNodePath = $_CONF->getKeyList();
				
				$_CONF->setValue('CROSS.table', $_GET['crossTable'], $crossNodePath);
				$_CONF->setValue('CROSS.table='.$_GET['crossTable'].'.ref', $_GET['crossTableRef'], $crossNodePath);
				$_CONF->setValue('CROSS.table='.$_GET['crossTable'].'.title', $_GET['crossTableLabel'], $crossNodePath);
				
				_log("Configurator Cross Field: Salvata nuova impostazione");
				$_CONF->saveBuiltXML();
				
				echo "<h3>Salvato</h3><script>parent.document.configurator.submit();</script>";
			}
			?>
			<div align="right"><input type="button" name="invia" value="IMPOSTA" onClick="window.location = '?field=<?=$_GET['field']?>&crossTable=<?=$_GET['crossTable']?>&crossTableRef=<?=$_GET['crossTableRef']?>&crossTableLabel=<?=$_GET['crossTableLabel']?>&action=SAVE'"></div>
		</div>
	<? } ?>
	
</body>
</html>
